import Vue from 'vue'
import Vuex from 'vuex'
import * as actions from './actions'
import * as getters from './getters'

import { getToken, setToken, removeToken } from '../utils/auth'



import {opsRouters,developerRouters,testerRouters} from '../routes'


Vue.use(Vuex)

// 应用初始状态
const state = {
    count: 10,
    user: {
        token: getToken(),
        name: '',
        department:"",
        role:"",

        avatar: '',
        // roles: '',
        isLogin: false,  //是否已加载路由
        // isLock: getStore({
        //     name: 'isLock'
        // }) || false,
        // lockPasswd: getStore({
        //     name: 'lockPasswd'
        // }) || '',
        // browserHeaderTitle: getStore({
        //     name: 'browserHeaderTitle'
        // }) || 'NxAdmin'
    },
    testerRoute: testerRouters,
    adminRoute: opsRouters,
    devRoute: developerRouters,
}

const stateBackup = {
    count: 10,
    user: {
        token: "",
        name: '',
        department:"",
        role:"",
        btnLimit:{},

        avatar: '',
        // roles: '',
        isLogin: false,  //是否已加载路由
        // isLock: getStore({
        //     name: 'isLock'
        // }) || false,
        // lockPasswd: getStore({
        //     name: 'lockPasswd'
        // }) || '',
        // browserHeaderTitle: getStore({
        //     name: 'browserHeaderTitle'
        // }) || 'NxAdmin'
    },
    testerRoute: testerRouters,
    adminRoute: opsRouters,
    devRoute: developerRouters,
}

// 定义所需的 mutations
const mutations = {
    INCREMENT(state) {
        state.count++
    },
    DECREMENT(state) {
        state.count--
    },
    SET_TOKEN: (state, token) => {
        state.user.token = token
    },
    SET_LOGIN_IN(state) {
        // console.log("set login in")
        state.user.isLogin = true
    },
    SET_LOGIN_OUT(state) {
        // console.log("set login in")
        // state = stateBackup
        // state.user = {}
        // state.testerRoute = ""
        // state.adminRoute = ""
        // state.devRoute = ""

    },

    SAVE_USER_INFO(state,user) {
        console.log("thie user is ...........")
        console.log(user)

        state.user.name = user.name
        state.user.department = user.department
        state.user.role = user.role
    },
    SAVE_USER_BTNLIMIT(state,btnLimit) {
        console.log("thie btnLimit is ...........")
        console.log(btnLimit)

        state.user.btnLimit = btnLimit
        // state.user.department = user.department
        // state.user.role = user.role
    },

}



// 创建 store 实例
export default new Vuex.Store({
    actions,
    getters,
    state,
    // state:sessionStorage.getItem('state') ? JSON.parse(sessionStorage.getItem('state')): {
    //     //id
    //     // skillId:'',
    //     // //技能状态
    //     // checkStatus:''
    // },
    mutations
})